<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JS计算数组元素总和</title>
    <script>
        var array = [1, 2, 3, 4, 5];

        // 求数组元素总和
        var sum = array.reduce(function (a, b) {
            return a + b;
        }, 0);

        // alert(sum);
        function f() {
            var val = document.getElementById('ttext');
            var value = val.value;
            var split = value.split(",");
            var i = 0;
            //2.在循环中设置一个条件表达式
            var num = 0;
            while (i < split.length) {
                num +=parseInt(split[i]);
                //3。定义一个更新表达式
                i++;
            }
            alert(value+"的和是："+num)
        }
    </script>
</head>
<body>
字符串（1,2,3）：<input type="text" id="ttext" value="">
<input type="button" value="求和" onclick="f()">
</body>
</html>